# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     __init__.py.py
   Description :   读取ini中的测试数据
   Author :        ZhangYiLin
   date：          2020/12/01 12:57
-------------------------------------------------
   Change Activity:
                   2020/12/01 12:57
-------------------------------------------------
"""
import os
import configparser
from config.ConfUi import ROOT_DIR


class ParseConFile(object):
    """
    读取ini中的测试数据
    """
    def __init__(self, module, page):
        print(module, page)
        self.file = os.path.join(ROOT_DIR, module, page + '.ini')
        print(self.file)
        self.conf = configparser.ConfigParser()
        self.conf.read(self.file, encoding='utf-8')

    def get_all_sections(self):
        """获取所有的section，返回一个列表"""
        return self.conf.sections()

    def get_all_options(self, section):
        """获取指定section下所有的option, 返回列表"""
        return self.conf.options(section)

    def get_locators_or_account(self, section, option):
        """获取指定section, 指定option对应的数据, 返回元祖和字符串"""
        try:
            locator = self.conf.get(section, option)
            if '->' in locator:
                locator = tuple(locator.split('->'))
            return locator
        except configparser.NoOptionError as e:
            print('error:', e)
        return 'error: No option "{}" in section: "{}"'.format(option, section)

    def get_option_value(self, section):
        """获取指定section下所有的option和对应的数据，返回字典"""
        value = dict(self.conf.items(section))
        return value


if __name__ == '__main__':
    a = ParseConFile('demo', 'ui_demo')
    print(a.get_all_sections())
    print(a.get_all_options('LoginPageElements'))
    print(a.get_option_value('LoginPageElements'))
